package me.numark.fragments;

import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.app.Dialog;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.joooonho.SelectableRoundedImageView;
import com.squareup.picasso.Picasso;

import me.numark.R;
import me.numark.activities.BaseActivity;
import me.numark.activities.LogInActivity;
import me.numark.activities.WebviewActivity;
import me.numark.model.Message;
import me.numark.view.CircleImageView;

/**
 * Created by Yao on 2015/10/12.
 */
public class AskToWebViewFragment extends DialogFragment{

    private TextView errorMessage;
    private int type;
    public static AskToWebViewFragment show(BaseActivity activity,int type) {
        AskToWebViewFragment newFragment = new AskToWebViewFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("type",type);
        newFragment.setArguments(bundle);
        newFragment.show(activity.getSupportFragmentManager(), "askloginDialog");
        return  newFragment;
    }


    public static void dismissDelay(AskToWebViewFragment fragment){

        fragment.mHandler.sendEmptyMessageDelayed(94,1500);
    }

    /**
     * private void doToast() {
     toastMsg.setVisibility(View.VISIBLE);
     Animator animator = AnimatorInflater.loadAnimator(this, R.animator.update_msg_anim);
     animator.setTarget(toastMsg);
     animator.start();
     mHandler.sendEmptyMessageDelayed(96, 3000);
     }
     * @param savedInstanceState
     * @return
     */

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        type = getArguments().getInt("type");
        final Dialog lDialog = new Dialog(getActivity(), R.style.dialog_style);
        lDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        lDialog.setContentView(R.layout.r_okcanceldialogview);
        SelectableRoundedImageView imageView = (SelectableRoundedImageView)lDialog.findViewById(R.id.dialog_imageview);
        imageView.setVisibility(View.VISIBLE);
        TextView dialogMessage = (TextView) lDialog.findViewById(R.id.dialog_message);
        View dialogDivider = lDialog.findViewById(R.id.dialog_devider);
        final EditText editText = (EditText)lDialog.findViewById(R.id.dialog_edit);
        editText.setVisibility(View.VISIBLE);
        dialogMessage.setVisibility(View.GONE);

        String text = null;
        ClipboardManager clipboard = (ClipboardManager)getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
        if (clipboard.hasPrimaryClip() && clipboard.getPrimaryClip().getItemCount() > 0) {
            ClipData.Item item = clipboard.getPrimaryClip().getItemAt(0);
            if (item.getText() != null) {
                text = item.getText().toString();
            }
        }
        Button buttonCancel =  (Button) lDialog.findViewById(R.id.cancel);
        Button buttonOK = (Button) lDialog.findViewById(R.id.ok);
        errorMessage = (TextView) lDialog.findViewById(R.id.error_msg);
        buttonOK.setText("立即推荐");

        if (type==0) {
            editText.setText(text);

            errorMessage.setVisibility(View.INVISIBLE);
        }else if(type==1){
            editText.setVisibility(View.GONE);
            buttonOK.setVisibility(View.GONE);
            buttonCancel.setVisibility(View.GONE);
            errorMessage.setVisibility(View.GONE);
            dialogDivider.setVisibility(View.GONE);
            dialogMessage.setVisibility(View.VISIBLE);
            dialogMessage.setText("快去复制一篇好文章的链接吧");
            buttonCancel.setText("确定");
        }



        buttonCancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        AskToWebViewFragment.this.dismiss();
                    }
                });


        buttonOK.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String url = editText.getText().toString().trim();
                        if (url != null && url.trim().startsWith("http")) {
                            AskToWebViewFragment.this.dismiss();
                            Intent intent = new Intent(getActivity(), WebviewActivity.class);
                            intent.putExtra(WebviewActivity.EXTRA_URL, url);
                            intent.putExtra(WebviewActivity.FROM_MAIN, true);
                            startActivity(intent);
                        }else if (!(url.trim().startsWith("http"))&&url.length()>0){
                            errorMessage.setText("地址错误！");
                            errorMessage.setVisibility(View.VISIBLE);
                            Animator animator = AnimatorInflater.loadAnimator(getActivity(), R.animator.update_msg_anim);
                            animator.setTarget(errorMessage);
                            animator.start();
                            mHandler.sendEmptyMessageDelayed(93, 3000);
                        }else if (TextUtils.isEmpty(url.trim())){
                            editText.setText("请先复制个链接吧~");
                            errorMessage.setVisibility(View.VISIBLE);
                            Animator animator = AnimatorInflater.loadAnimator(getActivity(), R.animator.update_msg_anim);
                            animator.setTarget(errorMessage);
                            animator.start();
                            mHandler.sendEmptyMessageDelayed(93, 3000);
                        }
                    }
                });
        return lDialog;
    }


    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(android.os.Message msg)
        {
            switch (msg.what) {
                case 93:
                    errorMessage.setVisibility(View.INVISIBLE);
                    break;
                case 94:
                    AskToWebViewFragment.this.dismiss();
                    break;
            }
        }
    };
}
